let {BASE_URL,ID,KEY} = require('../config/index')

// const base = 'https://dhujemqh.lc-cn-n1-shared.com'  //请务必换成自己的域名

function $fetch(url,method="GET",data={},header={}){
    return new Promise((resolve,reject)=>{
        wx.request({
            method,
            url: BASE_URL+url,
            header: { 
              'X-LC-Id': ID, //请务必换成自己的ID 
              'X-LC-Key': KEY,  //请务必换成自己的Key
              'Content-Type': ' application/json',
              ...header
            },
            data,
            success:(res)=>{
                resolve(res)  //将请求结果传递给then
            },
            fail:(err)=>{
                reject(err)  //将失败信息传递给catch
            }
        })
    })
}

function $get(url,data={}){
    return $fetch(url,'GET',data)
}
function $post(url,data={}){
    return $fetch(url,'POST',data)
}
function $del(url,data={}){
    return $fetch(url,'DELETE',data)
}
function $put(url,data={},header={}){
    return $fetch(url,'PUT',data,header)
}

module.exports = {
    $fetch,
    $get,
    $post,
    $del,
    $put
}